<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>课程列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/css/demo.css}">
    <script type="text/javascript" th:src="@{/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript" th:src="@{/easyui/js/validateExtends.js}"></script>
    <script type="text/javascript" th:inline="javascript">
        /*<![CDATA[*/
        $(function() {
            //datagrid初始化
            $('#dataList').datagrid({
                title:'课程列表',
                iconCls:'icon-more',//图标
                border: true,
                collapsible: false,//是否可折叠的
                fit: true,//自动大小
                method: "post",
                url:[[@{/course/getCourseList}]],
                idField:'id',
                singleSelect: false,//是否单选
                pagination: true,//分页控件
                rownumbers: true,//行号
                sortName:'id',
                sortOrder:'DESC',
                remoteSort: false,
                columns: [
                    [
                    {field:'chk',checkbox: true,width:50},
                    {field:'id',title:'ID',width:50, sortable: true},
                    {field:'name',title:'课程名称',width:200},
                    {field:'teacherId',title:'授课老师',width:200,
                        formatter: function(value,row,index){
                            if (row.teacherId){
                                var teacherList = $("#teacherList").combobox("getData");
                                for(var i=0;i<teacherList.length;i++ ){
                                    if(row.teacherId == teacherList[i].id)return teacherList[i].username;
                                }
                                return row.teacherId;
                            } else {
                                return 'not found';
                            }
                        }
                    },
                    {field:'courseDate',title:'上课时间',width:200},
                    {field:'selectedNum',title:'已选人数',width:200},
                    {field:'maxNum',title:'最大可选人数',width:200},
                ]
                ],
                toolbar: "#toolbar",
                onBeforeLoad : function(){
                    try{
                        $("#teacherList").combobox("getData")
                    }catch(err){
                        preLoadClazz();
                    }
                }
            });
            //提前加载教师信息
            function preLoadClazz(){
                $("#teacherList").combobox({
                    width: "150",
                    height: "25",
                    valueField: "id",
                    textField: "username",
                    multiple: false, //可多选
                    editable: false, //不可编辑
                    method: "post",
                    url: [[@{/teacher/getTeacherList(from=combox)}]],
                    onChange: function(newValue, oldValue){
                        //加载班级下的学生
                        //$('#dataList').datagrid("options").queryParams = {clazzid: newValue};
                        //$('#dataList').datagrid("reload");
                    }

                });
            }

            //设置分页控件
            var p = $('#dataList').datagrid('getPager');
            $(p).pagination({
                pageSize: 10,//每页显示的记录条数，默认为10
                pageList: [10,20,30,50,100],//可以设置每页记录条数的列表
                beforePageText: '第',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });

            //设置工具类按钮
            $("#add").click(function(){
                $("#addDialog").dialog("open");
            });

            //设置编辑按钮
            $("#edit").click(function(){
                table = $("#editTable");
                var selectRows = $("#dataList").datagrid("getSelections");
                if(selectRows.length != 1){
                    $.messager.alert("消息提醒", "请选择一条数据进行操作!", "warning");
                } else{
                    $("#editDialog").dialog("open");
                }
            });

            //编辑课程信息
            $("#editDialog").dialog({
                title: "修改课程信息",
                width: 450,
                height: 400,
                iconCls: "icon-edit",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text:'提交',
                        plain: true,
                        iconCls:'icon-user_add',
                        handler:function(){
                            var validate = $("#editForm").form("validate");
                            if(!validate){
                                $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                                return;
                            } else{
                                var teacherId = $("#edit_teacherList").combobox("getValue");
                                var id = $("#dataList").datagrid("getSelected").id;
                                var name = $("#edit_name").textbox("getText");
                                var courseDate = $("#edit_course_date").textbox("getText");
                                var maxNum = $("#edit_max_num").numberbox("getValue");
                                var info = $("#edit_info").val();
                                var data = {id:id, teacherId:teacherId, name:name,courseDate:courseDate,info:info,maxnum:maxNum};

                                $.ajax({
                                    type: "post",
                                    url: [[@{/course/editCourse}]],
                                    data: data,
                                    success: function(data){
                                        if(data.success){
                                            $.messager.alert("消息提醒",data.message,"info");
                                            //关闭窗口
                                            $("#editDialog").dialog("close");
                                            //清空原表格数据
                                            $("#edit_name").textbox('setValue', "");
                                            $("#edit_course_date").textbox('setValue', "");
                                            $("#edit_info").val("");

                                            //重新刷新页面数据
                                            $('#dataList').datagrid("reload");
                                            $('#dataList').datagrid("uncheckAll");

                                        } else{
                                            $.messager.alert("消息提醒",data.message,"warning");
                                            return;
                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        text:'重置',
                        plain: true,
                        iconCls:'icon-reload',
                        handler:function(){
                            $("#edit_name").textbox('setValue', "");
                            $("#edit_phone").textbox('setValue', "");
                            $("#edit_qq").textbox('setValue', "");

                            $(table).find(".chooseTr").remove();

                        }
                    },
                ],
                onBeforeOpen: function(){
                    var selectRow = $("#dataList").datagrid("getSelected");
                    //设置值
                    $("#edit_name").textbox('setValue', selectRow.name);
                    $("#edit_course_date").textbox('setValue', selectRow.courseDate);
                    $("#edit_max_num").numberbox('setValue', selectRow.maxNum);
                    $("#edit_info").val(selectRow.info);
                    //$("#edit-id").val(selectRow.id);
                    var teacherId = selectRow.teacherId;
                    setTimeout(function(){
                        $("#edit_teacherList").combobox('setValue', teacherId);
                    }, 100);
                },
                onClose: function(){
                    $("#edit_name").textbox('setValue', "");
                    $("#edit_course_date").textbox('setValue', "");
                    $("#edit_info").val("");
                    //$("#edit-id").val('');
                }
            });

            //删除
            $("#delete").click(function(){
                var selectRow = $("#dataList").datagrid("getSelections");
                if(selectRow == null){
                    $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
                } else{
                    var jsonObj = {};
                    $.each(selectRow,function(i,n){
                        jsonObj["ids["+i+"]"] = n.id;
                    });
                    $.messager.confirm("消息提醒", "将删除与课程相关的所有数据，确认继续？", function(r){
                        if(r){
                            $.ajax({
                                type: "post",
                                url: [[@{/course/deleteCourse}]],
                                data: jsonObj,
                                success: function(data){
                                    if(data.message){
                                        $.messager.alert("消息提醒",data.success,"info");
                                        //刷新表格
                                        $("#dataList").datagrid("reload");
                                    } else{
                                        $.messager.alert("消息提醒",data.message,"warning");
                                        return;
                                    }
                                }
                            });
                        }
                    });
                }
            });

            //设置添加窗口
            $("#addDialog").dialog({
                title: "添加课程",
                width: 450,
                height: 400,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text:'添加',
                        plain: true,
                        iconCls:'icon-book-add',
                        handler:function(){
                            var validate = $("#addForm").form("validate");
                            if(!validate){
                                $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                                return;
                            } else{
                                $.ajax({
                                    type: "post",
                                    url: [[@{/course/addCourse}]],
                                    data: $("#addForm").serialize(),
                                    success: function(data){
                                        if(data.success){
                                            $.messager.alert("消息提醒",data.message,"info");
                                            //关闭窗口
                                            $("#addDialog").dialog("close");
                                            //清空原表格数据
                                            $("#add_name").textbox('setValue', "");
                                            //刷新
                                            $('#dataList').datagrid("reload");
                                        } else{
                                            $.messager.alert("消息提醒",data.message,"warning");
                                            return;
                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        text:'重置',
                        plain: true,
                        iconCls:'icon-book-reset',
                        handler:function(){
                            $("#add_name").textbox('setValue', "");
                        }
                    },
                ]
            });

            //下拉框通用属性
            $("#add_teacherList, #edit_teacherList,#teacherList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "username",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
            });
            //添加信息教师选择框
            $("#add_teacherList").combobox({
                url: [[@{/teacher/getTeacherList(from=combox)}]],
                onLoadSuccess: function(){
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                }
            });
            //编辑信息教师选择框
            $("#edit_teacherList").combobox({
                url: [[@{/teacher/getTeacherList(from=combox)}]],
                onLoadSuccess: function(){
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                }
            });

            //搜索按钮监听事件
            $("#search-btn").click(function(){
                $('#dataList').datagrid('load',{
                    name: $('#courseName').val(),
                    teacherid: $("#teacherList").combobox('getValue') == '' ? 0 : $("#teacherList").combobox('getValue')
                });
            });
        });
        /*]]>*/
    </script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">

</table>
<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="edit" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a></div>
    <div style="float: left; margin-right: 10px;"><a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="margin-top: 3px;">
        课程名称：<input id="courseName" class="easyui-textbox" name="clazzName" />
        授课老师：<input id="teacherList" class="easyui-textbox" name="clazz" />
        <a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
    </div>
</div>

<!-- 添加数据窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8" >
            <tr>
                <td>课程名称:</td>
                <td><input id="add_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="name" data-options="required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td style="width:40px">授课老师:</td>
                <td colspan="3">
                    <input id="add_teacherList" style="width: 200px; height: 30px;" class="easyui-textbox" name="teacherId" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td>上课时间:</td>
                <td><input id="add_course_date" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="courseDate" data-options="required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td>最多可选人数:</td>
                <td><input id="add_max_num" style="width: 200px; height: 30px;" class="easyui-numberbox" type="text" name="maxNum" data-options="min:0,precision:0,required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td>课程介绍:</td>
                <td>
                    <textarea id="info" name="info" style="width: 200px; height: 60px;" class="" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- 编辑数据窗口 -->
<div id="editDialog" style="padding: 10px">
    <form id="editForm" method="post">
        <!-- <input type="hidden" name="id" id="edit-id"> -->
        <table cellpadding="8" >
            <tr>
                <td>课程名称:</td>
                <td><input id="edit_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="name" data-options="required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td style="width:40px">授课老师:</td>
                <td colspan="3">
                    <input id="edit_teacherList" style="width: 200px; height: 30px;" class="easyui-textbox" name="teacherid" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td>上课时间:</td>
                <td><input id="edit_course_date" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="course_date" data-options="required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td>最多可选人数:</td>
                <td><input id="edit_max_num" style="width: 200px; height: 30px;" class="easyui-numberbox" type="text" name="max_num" data-options="min:0,precision:0,required:true, missingMessage:'不能为空'" /></td>
            </tr>
            <tr>
                <td>课程介绍:</td>
                <td>
                    <textarea id="edit_info" name="info" style="width: 200px; height: 60px;" class="" ></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>